package org.msfj.hospital.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.msfj.hospital.model.HospitalDept;
import org.msfj.hospital.model.HospitalExpert;
import org.msfj.hospital.service.HospitalDeptService;
import org.msfj.hospital.service.HospitalExpertService;
import org.msfj.hospital.vo.HospitalDeptVo;
import org.msfj.hospital.vo.HospitalExpertVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 医院专家信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2018-08-10 14:12:03
 * @Version: 1.0
 * @Created in idea by autoCode
 */
@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "hospitalExpert")
public class HospitalExpertServiceImpl extends BaseServiceImpl<HospitalExpert> implements HospitalExpertService {

    @Autowired
    HospitalDeptService hospitalDeptService;

    /**
     * 根据条件分页查询医院专家信息列表
     *
     * @param hospitalExpertVo
     * @param page             当前第几页
     * @param rows             每页多少条记录
     * @return PageInfo<HospitalExpert>
     */
    @Override
    //@Cacheable
    public PageInfo<HospitalExpertVo> getByHospitalExpert(HospitalExpertVo hospitalExpertVo, int page, int rows) {
        Example example = new Example(HospitalExpert.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        if (StrUtil.isNotBlank(hospitalExpertVo.getDeptId())) {
            criteria.andEqualTo("deptId",hospitalExpertVo.getDeptId());
        }
        PageHelper.startPage(page, rows);
        List<HospitalExpert> hospitalExpertList = super.getByExample(example);
        PageInfo hospitalExpertVoPageInfo = new PageInfo(hospitalExpertList);
        hospitalExpertVoPageInfo.setList(setOtherProperties(hospitalExpertList));
        return hospitalExpertVoPageInfo;
    }

    /**
     * 根据条件查询所有医院专家信息对象列表
     *
     * @param hospitalExpertVo
     * @return List<HospitalExpert>
     */
    @Override
    //@Cacheable
    public List<HospitalExpertVo> getAllHospitalExpert(HospitalExpertVo hospitalExpertVo) {
        Example example = new Example(HospitalExpert.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        List<HospitalExpertVo> hospitalExpertVoList = setOtherProperties(super.getByExample(example));
        return hospitalExpertVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param hospitalExpert
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public HospitalExpertVo setOtherProperties(HospitalExpert hospitalExpert) {
        HospitalExpertVo hospitalExpertVo = new HospitalExpertVo();
        BeanUtils.copyProperties(hospitalExpert, hospitalExpertVo);
        if (StrUtil.isNotBlank(hospitalExpert.getDeptId())) {
            HospitalDept byId = hospitalDeptService.getById(hospitalExpert.getDeptId());
            hospitalExpertVo.setHospitalDeptVo(hospitalDeptService.setOtherProperties(byId));
        }
        return hospitalExpertVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param hospitalExperts
     * @return List<HospitalExpertVo>
     */
    @Override
    //@Cacheable
    public List<HospitalExpertVo> setOtherProperties(List<HospitalExpert> hospitalExperts) {
        List<HospitalExpertVo> hospitalExpertVos = new ArrayList<>();
        List<HospitalDeptVo> allHospitalDept = hospitalDeptService.getAllHospitalDept(null);
        Map<String, HospitalDeptVo> hospitalDeptVoMap = CollectionUtil.newHashMap();
        for (HospitalDeptVo hospitalDeptVo : allHospitalDept) {
            hospitalDeptVoMap.put(hospitalDeptVo.getId(), hospitalDeptVo);
        }
        if (CollectionUtil.isNotEmpty(hospitalExperts)) {
            for (HospitalExpert hospitalExpert : hospitalExperts) {
                HospitalExpertVo hospitalExpertVo = new HospitalExpertVo();
                BeanUtils.copyProperties(hospitalExpert, hospitalExpertVo);
                if (StrUtil.isNotBlank(hospitalExpertVo.getDeptId())) {
                    hospitalExpertVo.setHospitalDeptVo(hospitalDeptVoMap.get(hospitalExpertVo.getDeptId()));
                }
                hospitalExpertVos.add(hospitalExpertVo);
            }
        }
        return hospitalExpertVos;
    }

    @Override
    //@Cacheable
    public Map<String,Integer> getHospitalDeptExpertCountMap() {
        Map<String,Integer> hospitalDeptExpertCountMap = MapUtil.newHashMap();
        Example example = new Example(HospitalExpert.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        List<HospitalExpert> byExample = super.getByExample(example);
        if(CollectionUtil.isNotEmpty(byExample)){
            for (HospitalExpert hospitalExpert : byExample) {
                String deptId = hospitalExpert.getDeptId();
                Integer integer = hospitalDeptExpertCountMap.get(deptId);
                if(ObjectUtil.isNotNull(integer)){
                    integer = integer + 1;
                    hospitalDeptExpertCountMap.remove(deptId);
                    hospitalDeptExpertCountMap.put(deptId,integer);
                }else{
                    hospitalDeptExpertCountMap.put(deptId,1);
                }
            }
        }
        return hospitalDeptExpertCountMap;
    }


}
